package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.KLog;


public class SetAliWeChatPayAccountViewModel extends BaseViewModel {
    //真实姓名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //支付宝或者微信账号的绑定
    public ObservableField<String> userAccount = new ObservableField<>("");
    //微信和支付宝的提示信息
    public ObservableField<String> tipsInfo = new ObservableField<>("支付宝");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt(View.INVISIBLE);
    public ObservableInt clearAccountBtnVisibility = new ObservableInt(View.INVISIBLE);


    public SetAliWeChatPayAccountViewModel(@NonNull Application application) {
        super(application);
    }

    //1，支付；2，微信；
    private int type = 1;

    //清除用户名的点击事件, 逻辑从View层转换到ViewModel层
    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userName.set("");
        }
    });
    public BindingCommand clearUserAccountOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            userAccount.set("");
        }
    });
    //用户名长度改变的回调事件
    public BindingCommand onTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearBtnVisibility.set(View.INVISIBLE);
            } else {
                clearBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userName.get())) {
                clearBtnVisibility.set(View.VISIBLE);
            } else {
                clearBtnVisibility.set(View.INVISIBLE);
            }
        }
    });
    //用户名长度改变的回调事件
    public BindingCommand onAccountTextChangeCommand = new BindingCommand<>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            KLog.d("onTextChangeCommand==" + s);
            if (TextUtils.isEmpty(s)) {
                clearAccountBtnVisibility.set(View.INVISIBLE);
            } else {
                clearAccountBtnVisibility.set(View.VISIBLE);
            }
        }
    });
    //用户名输入框焦点改变的回调事件
    public BindingCommand<Boolean> onAccountFocusChangeCommand = new BindingCommand<>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean hasFocus) {
            if (hasFocus && !TextUtils.isEmpty(userAccount.get())) {
                clearAccountBtnVisibility.set(View.VISIBLE);
            } else {
                clearAccountBtnVisibility.set(View.INVISIBLE);
            }
        }
    });

    //提交按钮的点击事件
    public BindingCommand confirmOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });


    public void setBundleInfo(Bundle bundle) {
        if (bundle != null) {
            type = bundle.getInt("type", 1);
            if (type == 1) {
                tipsInfo.set("支付宝");
            } else {
                tipsInfo.set("微信");
            }
        }
    }
}
